package ferran.alcazar.one;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

/**
 * @author Tomas
 * 
 */
public class MapaActivity extends MapActivity {
	private MapController mapController;
	private MapView map = null;
	public GeoPoint miPosicion;

	@Override
	protected void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.maps);

		map = (MapView) findViewById(R.id.map);
		map.setBuiltInZoomControls(true);

		mapController = map.getController();
		mapController.setZoom(17);

		/* Use the LocationManager class to obtain GPS locations */
		LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		/* minDistante = 10m, minTime = 10s for notifications */
		mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000,
				10, new GeoUpdateHandler());

		MyLocationOverlay me = new MyLocationOverlay();
		map.getOverlays().add(me);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_S) {
			map.setSatellite(!map.isSatellite());
			return true;
		} else if (keyCode == KeyEvent.KEYCODE_Z) {
			map.displayZoomControls(true);
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.google.android.maps.MapActivity#isRouteDisplayed()
	 */
	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	public class GeoUpdateHandler implements LocationListener {

		@Override
		public void onLocationChanged(Location location) {
			int lat = (int) (location.getLatitude() * 1E6);
			int lng = (int) (location.getLongitude() * 1E6);

			miPosicion = new GeoPoint(lat, lng);
			mapController.animateTo(miPosicion); // mapController.setCenter(point);
		}

		@Override
		public void onProviderDisabled(String provider) {
			Toast.makeText(getApplicationContext(), provider + " Disabled",
					Toast.LENGTH_SHORT).show();
		}

		@Override
		public void onProviderEnabled(String provider) {
			Toast.makeText(getApplicationContext(), provider + " Enabled",
					Toast.LENGTH_SHORT).show();
		}

		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
		}
	}

	public class MyLocationOverlay extends Overlay {

		@Override
		public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
				long when) {
			super.draw(canvas, mapView, shadow);

			if (miPosicion != null) {
				Paint paint = new Paint();
				Point myScreenCoords = new Point();
				mapView.getProjection().toPixels(miPosicion, myScreenCoords);
				paint.setStrokeWidth(1);
				paint.setARGB(255, 0, 0, 0);
				paint.setStyle(Paint.Style.STROKE);
				Bitmap bmp = BitmapFactory.decodeResource(getResources(),
						R.drawable.marker);
				canvas.drawBitmap(bmp, myScreenCoords.x, myScreenCoords.y,
						paint);
				canvas.drawText(getString(R.string.mylocation),
						myScreenCoords.x, myScreenCoords.y, paint);
				return true;
			}

			return false;
		}
	}

	public class LocationWinOverLay extends Overlay {
		private GeoPoint loc;

		public LocationWinOverLay(GeoPoint Loc) {
			this.loc = Loc;
		}

		@Override
		public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
				long when) {
			super.draw(canvas, mapView, shadow);

			Paint paint = new Paint();
			Point myScreenCoords = new Point();
			mapView.getProjection().toPixels(loc, myScreenCoords);
			paint.setStrokeWidth(1);
			paint.setARGB(255, 0, 0, 0);
			paint.setStyle(Paint.Style.STROKE);
			Bitmap bmp = BitmapFactory.decodeResource(getResources(),
					R.drawable.marker);
			canvas.drawBitmap(bmp, myScreenCoords.x, myScreenCoords.y, paint);
			return true;
		}
	}

}
